<?php
/**
 * Leek - Zend Framework Components
 *
 * @category   Leek
 * @package    Leek_Application
 * @subpackage Resource
 * @author     Chris Jones <leeked@gmail.com>
 * @link       http://code.google.com/p/leek-zf-components/
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: Acl.php 52 2009-06-02 22:11:31Z leeked $
 */

/**
 * Resource for setting up Zend_Acl
 *
 * @uses       Zend_Application_Resource_ResourceAbstract
 * @category   Leek
 * @package    Leek_Application
 * @subpackage Resource
 * @author     Chris Jones <leeked@gmail.com>
 * @link       http://code.google.com/p/leek-zf-components/
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
class Leek_Application_Resource_Acl extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Zend_Controller_Front
     */
    protected $_front;

    /**
     * @var Leek_Acl
     */
    protected $_acl;

    /**
     * Sets the field of Zend_Auth to use for role
     *
     * @return Leek_Application_Resource_Acl
     */
    public function setAuthRoleField($authRoleField)
    {
        $this->getAcl()->setAuthRoleField($authRoleField);
        return $this;
    }

    /**
     * Sets the resources files filetype
     *
     * @return Leek_Application_Resource_Router
     */
    public function setResourceFilesType($type)
    {
        $this->getAcl()->setResourceFilesType($type);
        return $this;
    }

    /**
     * Set ACL config file and load roles/resources
     *
     * @param string $filePath
     * @return Leek_Application_Resource_Acl
     */
    public function setConfigPath($filePath)
    {
        $config = Leek_Config::loadConfig($filePath);

        // Load roles
        if (isset($config['roles'])) {
            $this->setRoles($config['roles']);
        } else {
            throw new Zend_Application_Resource_Exception('No roles key in role file.');
        }

        // Load resources (if applicable)
        if (isset($config['resources'])) {
            $this->setResources($config['resources']);
        }

        return $this;
    }

    /**
     * Sets/Adds resources from a config
     *
     * @param array $resources
     * @return Leek_Application_Resource_Acl
     */
    public function setResources(array $resources)
    {
        $this->getAcl()->addResources($resources);
        return $this;
    }

   /**
     * Sets the paths where we can find resources
     *
     * @return Leek_Application_Resource_Acl
     */
    public function setResourcesDirectory($resourcesDirectory)
    {
        if (is_string($resourcesDirectory)) {
            $this->getAcl()->addResourcesDirectory($resourcesDirectory, $this->getFrontController()->getDefaultModule());
        }
        return $this;
    }

    /**
     * Adds roles specified in config
     *
     * @param array $roles
     * @return Leek_Application_Resource_Acl
     */
    public function setRoles(array $roles)
    {
        $this->getAcl()->addRoles($roles);
        return $this;
    }

    /**
     * Sets the deny default options
     *
     * @param array $options
     * @return Leek_Application_Resource_Acl
     */
    public function setDenyRedirectDefaults(array $options)
    {
        foreach ($options as $key => $value) {
            $this->getAcl()->setDenyRedirectDefaultOption($key, $value);
        }

        return $this;
    }

    /**
     * Sets the no auth default options
     *
     * @param array $options
     * @return Leek_Application_Resource_Acl
     */
    public function setNoAuthRedirectDefaults(array $options)
    {
        foreach ($options as $key => $value) {
            $this->getAcl()->setNoAuthRedirectDefaultOption($key, $value);
        }

        return $this;
    }

    /**
     * Defined by Zend_Application_Resource_ResourceAbstract
     *
     * @return void
     */
    public function init()
    {
        $this->getBootstrap()->bootstrap('Frontcontroller');

        $acl     = $this->getAcl();
        $options = $this->getOptions();

        foreach ($options as $key => $value) {
            switch (strtolower($key)) {

                case 'modules':
                    $modules       = $this->getFrontController()->getControllerDirectory();
                    $moduleOptions = array_change_key_case($value, CASE_LOWER);
                    foreach ($moduleOptions as $moduleKey => $moduleValue) {
                        switch ($moduleKey) {

                            // Sets the directory suffix to load module resources from
                            case 'resourcesdirectorysuffix':
                                foreach ($modules as $moduleName => $modulePath) {
                                    $moduleResourcePath = realpath($modulePath . '/../' . $moduleValue);
                                    if ($moduleResourcePath) {
                                        $acl->addResourcesDirectory($moduleResourcePath, $moduleName);
                                    }
                                }
                                break;

                        }
                    }
                    break;

            }
        }
    }

    /**
     * Retrieve ACL instance
     *
     * @return Leek_Acl
     */
    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->_acl = Leek_Acl::getInstance();
        }
        return $this->_acl;
    }

    /**
     * Retrieve front controller instance
     *
     * @return Zend_Controller_Front
     */
    public function getFrontController()
    {
        if (null === $this->_front) {
            $this->_front = Zend_Controller_Front::getInstance();
        }
        return $this->_front;
    }
}
